﻿using ETC.DLEDLightingSystemPlus.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace ETC.DLEDLightingSystemPlus.Models
{
    public class TestInfo : Bandit.UI.MVVM.NotifyObject
    {
        private string _StationName;

        public string StationName
        {
            get { return _StationName; }
            set { _StationName = value; OnPropertyChanged("StationName"); }
        }

        #region TestItems
        private ObservableCollection<TestItemResult> _TestItemResults;
        /// <summary>
        /// Pass Count
        /// </summary>
        public ObservableCollection<TestItemResult> TestItemResults
        {
            get { return _TestItemResults; }
            set
            {
                _TestItemResults = value;
                OnPropertyChanged("TestItemResults");
            }
        }
        #endregion

        #region Name
        private string _Name;
        /// <summary>
        /// Pass Count
        /// </summary>
        public string Name
        {
            get { return _Name; }
            set
            {
                _Name = value;
                OnPropertyChanged("Name");
            }
        }
        #endregion

        #region Pass
        private int _Pass;
        /// <summary>
        /// Pass Count
        /// </summary>
        public int Pass
        {
            get { return _Pass; }
            set
            {
                _Pass = value;
                OnPropertyChanged("Pass");
            }
        }
        #endregion

        #region Fail
        private int _Fail;
        /// <summary>
        /// Fail Count
        /// </summary>
        public int Fail
        {
            get { return _Fail; }
            set
            {
                _Fail = value;
                OnPropertyChanged("Fail");
            }
        }
        #endregion

        #region Total
        private int _Total;
        /// <summary>
        /// Total Count
        /// </summary>
        public int Total
        {
            get { return _Total; }
            set
            {
                _Total = value;
                OnPropertyChanged("Total");
            }
        }
        #endregion

        #region Barcode
        private string _Barcode;
        /// <summary>
        /// Barcode
        /// </summary>
        public string Barcode
        {
            get { return _Barcode; }
            set
            {
                _Barcode = value;
                OnPropertyChanged("Barcode");
            }
        }
        #endregion

        #region BarcodeIsReadOnly

        private bool _BarcodeIsReadOnly;
        /// <summary>
        /// Barcode文本框是否只读
        /// </summary>
        public bool BarcodeIsReadOnly
        {
            get { return _BarcodeIsReadOnly; }
            set
            {
                _BarcodeIsReadOnly = value;
                OnPropertyChanged("BarcodeIsReadOnly");
            }
        }

        #endregion

        #region Result
        /// <summary>
        /// Result
        /// </summary>
        public TestResultState Result { get; private set; }

        /// <summary>
        /// Result Background
        /// </summary>
        public string ResultBackground { get; private set; }

        /// <summary>
        /// Result Icon
        /// </summary>
        public string ResultIcon { get; private set; }

        private System.Windows.Visibility _ResultVisibility;
        /// <summary>
        /// 结果显示状态
        /// </summary>
        public System.Windows.Visibility ResultVisibility
        {
            get { return _ResultVisibility; }
            set
            {
                _ResultVisibility = value;
                OnPropertyChanged("ResultVisibility");
            }
        }
        #endregion

        #region 进度
        private double _Progress;
        /// <summary>
        /// 测试进度
        /// </summary>
        public double Progress
        {
            get { return _Progress; }
            set
            {
                if (value == _Progress)
                {
                    return;
                }
                _Progress = value;
                OnPropertyChanged("Progress");
            }
        }

        private System.Windows.Visibility _ProgressBarVisibility;
        /// <summary>
        /// 进度条显示状态
        /// </summary>
        public System.Windows.Visibility ProgressBarVisibility
        {
            get { return _ProgressBarVisibility; }
            set
            {
                _ProgressBarVisibility = value;
                OnPropertyChanged("ProgressBarVisibility");
            }
        }
        #endregion

        #region 计时
        private string _TestTime;
        /// <summary>
        /// 测试时间
        /// </summary>
        public string TestTime
        {
            get { return _TestTime; }
            set
            {
                _TestTime = value;
                OnPropertyChanged("TestTime");
            }
        }

        public Bandit.Helpers.TestTimer TestTimer { get; private set; }
        public void Startwatch()
        {
            System.Threading.Tasks.Task.Factory.StartNew((Action)delegate
            {
                TestTimer.Start();

                while (TestTimer.IsRunning)
                {
                    this.TestTime = TestTimer.TestTime;

                    System.Threading.Thread.Sleep(5);
                }

                this.TestTime = TestTimer.TestTime;
            });
        }

        public void Stopwatch()
        {
            TestTimer.Stop();
        }
        #endregion

        #region EnabledStartButton
        private bool _EnabledStartButton = false;
        /// <summary>
        /// 启用或禁用开始按钮
        /// </summary>
        public bool EnabledStartButton
        {
            get { return _EnabledStartButton; }
            set
            {
                _EnabledStartButton = value;
                OnPropertyChanged("EnabledStartButton");
            }
        }
        #endregion

        #region IsTesting
        private bool _IsTesting;
        /// <summary>
        /// 是否测试中
        /// </summary>
        public bool IsTesting
        {
            get { return _IsTesting; }
            set
            {
                _IsTesting = value;
                OnPropertyChanged("IsTesting");
            }
        }

   
        #endregion

        public TestInfo()
        {
            this.Reset();
        }
        public TestInfo(string Name)
        {
            this._Name = Name;

            this._TestItemResults = new ObservableCollection<TestItemResult>(); 
            
            this.Reset();
        }
        /// <summary>
        /// 重置
        /// </summary>
        public void Reset()
        {
            this._Pass = 0;
            this._Fail = 0;
            this._Total = 0;
            this.UpdateResult(TestResultState.Idle);
            this._ResultVisibility = System.Windows.Visibility.Visible;
            this._Progress = 0.0;
            this._ProgressBarVisibility = System.Windows.Visibility.Collapsed;
            this._TestTime = "00:00:00.000";
            this.TestTimer = new Bandit.Helpers.TestTimer();
        }


        #region 更新结果
        private void UpdateResult(TestResultState result)
        {
            switch (result)
            {
                case TestResultState.Idle:
                    this.ResultBackground = MyBrushes.IDLE.ToBrushString();
                    this.ResultIcon = "Images/IDLE.png";
                    break;
                case TestResultState.Testing:
                    this.ResultBackground = MyBrushes.TESTING.ToBrushString();
                    this.ResultIcon = "Images/Testing.png";
                    break;
                case TestResultState.Pass:
                    this.Pass++;
                    this.Total++;
                    this.ResultBackground = MyBrushes.PASS.ToBrushString();
                    this.ResultIcon = "Images/Pass.png";
                    break;
                case TestResultState.Fail:
                    this.Fail++;
                    this.Total++;
                    this.ResultBackground = MyBrushes.FAIL.ToBrushString();
                    this.ResultIcon = "Images/Fail.png";
                    break;
                case TestResultState.Stop:
                    this.ResultBackground = MyBrushes.RichYellow.ToBrushString();
                    this.ResultIcon = "Images/Fail.png";
                    break;
                default:
                    break;
            }
            this.Result = result;
            OnPropertyChanged("Result");
            OnPropertyChanged("ResultBackground");
            OnPropertyChanged("ResultIcon");
        }
        #endregion

        private void StartProgress()
        {
            this.ResultVisibility = System.Windows.Visibility.Collapsed;
            this.Progress = 0.0;
            this.ProgressBarVisibility = System.Windows.Visibility.Visible;
        }

        private void EndProgress()
        {
            this.ProgressBarVisibility = System.Windows.Visibility.Collapsed;
            this.ResultVisibility = System.Windows.Visibility.Visible;
        }

        public void TestStart()
        {
            this.EnabledStartButton = false;
            this.IsTesting = true;
            this.StartProgress();
            this.UpdateResult(TestResultState.Testing);
            this.Startwatch();
            this.BarcodeIsReadOnly = true;
        }

        public void TestEnd(TestResultState result)
        {
            this.Stopwatch();
            this.EnabledStartButton = true;
            this.IsTesting = false;
            this.UpdateResult(result);
            this.EndProgress();
            this.BarcodeIsReadOnly = false;
        }
    }
}
